# -*- coding: utf-8 -*-

from qiniu import Auth, put_file, etag, urlsafe_base64_encode
import qiniu.config
from scrapy.utils.project import get_project_settings

class qiniuServer:
    bucket_name = 'dra100'

    def __new__(cls, *args, **kw):  
        if not hasattr(cls, '_instance'):  
            o = super(qiniuServer, cls)  
            cls._instance = o.__new__(cls, *args, **kw)  
        return cls._instance 

    def __init__(self):
        settings = get_project_settings()
        self.auth = Auth(settings['QINIU_ACCESS_KEY'], settings['QINIU_SECRET_KEY'])

    def upload(self, file_name, file_path):
        self.token = self.auth.upload_token(self.bucket_name, file_name, 3600)
        ret, info = put_file(self.token, file_name, file_path)
        if (info.status_code == 200):
            return True
        else:
            return False